//
//  RankPageViewController.m
//  MoGuRadio
//
//  Created by dlios on 15/6/3.
//  Copyright (c) 2015年 mogu.com 蘑菇科技. All rights reserved.
//

#import "RankPageViewController.h"
#import "SpecialPageViewController.h"
#import "SearchPageViewController.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "RankCell.h"
#import "TopCell.h"
#import "MJRefresh.h"
#import "LBView.h"
#import "YRSideViewController.h"
@interface RankPageViewController () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *rankTableV;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) NSDictionary *moguDic;
@property (nonatomic, retain) NSString *buttonImage1;
@property (nonatomic, retain) NSString *buttonImage2;

@end

@implementation RankPageViewController

- (void)dealloc
{

    [_buttonImage1 release];
    [_buttonImage2 release];
    [_rankTableV release];
    [_arr release];
    [_moguDic release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self JSONParser];
    [self setupViews];
    
<<<<<<< HEAD
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.097 green:0.6701 blue:0.4134 alpha:1.0];
    self.navigationController.navigationBar.translucent = YES;
    
=======
    //导航栏随机颜色
//     self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0  blue:arc4random() % 255 / 255.0 alpha:0.7];
//    
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"daohanglanlanlan.png"] forBarMetrics:UIBarMetricsDefault];
    
    
    self.navigationController.navigationBar.tintColor = [UIColor purpleColor];
    
    
    self.navigationController.navigationBar.translucent = YES;
>>>>>>> b3b04c99b972b34aa9c958defbf325ea74670e29
    // 下拉刷新
    [self.rankTableV addHeaderWithTarget:self action:@selector(JSONParser)];
    [self.rankTableV headerBeginRefreshing];
    [self JSONParser];
    
    UIImage *image2 = [UIImage imageNamed:@"liftFind@2x"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(barButtonAction2:)] autorelease];
    
<<<<<<< HEAD
    
    // 导航栏右侧按钮
    UIImage *rightButtonImage = [UIImage imageNamed:@"search123@3x"];
    rightButtonImage = [rightButtonImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:rightButtonImage style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction:)];
    
    
//    self.rankTableV.backgroundColor = [UIColor colorWithRed:0.88 green:0.90 blue:0.738 alpha:0.2];
    
    
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"daohanglanlanlan.png"]forBarMetrics:UIBarMetricsDefault];
=======
     // 添加TableView的背景图片
    
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"00003.jpg"]]];
    
    // 将TableView 设为透明色
    self.rankTableV.backgroundColor = [UIColor clearColor];
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    UIVisualEffectView *view = [[UIVisualEffectView alloc] initWithEffect:blur];
    
    self.view.frame = CGRectMake(0, 0, 100, 100);
    [self.rankTableV addSubview:view];
>>>>>>> b3b04c99b972b34aa9c958defbf325ea74670e29
    
    
}

- (void)barButtonAction2:(UIBarButtonItem *)barButton
{
    YRSideViewController *ZHAIRUI = (YRSideViewController *)self.view.window.rootViewController;
    [ZHAIRUI showLeftViewController:YES];
}

- (void)rightButtonAction:(UIBarButtonItem *)barButton
{
    SearchPageViewController *searchVC = [[SearchPageViewController alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}

// 创建TableView  TableView相关
- (void)setupViews
{
<<<<<<< HEAD
    UIImageView *headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, LBVIEW_WIDTH1, 150 * kYHeight)];
    NSString *imageStr = @"http://bt.img.17gwx.com/topic/0/5/d1Vb/800x360";
    [headerView setImageWithURL:[NSURL URLWithString:imageStr]];
    
=======
<<<<<<< HEAD
    self.rankTableV = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
//    self.rankTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)style:UITableViewStylePlain];
=======
    self.rankTableV = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    //self.rankTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)style:UITableViewStylePlain];
>>>>>>> c1f2daaa683b676696d15cbdc25e38c446ffa905
>>>>>>> b3b04c99b972b34aa9c958defbf325ea74670e29
    
    self.rankTableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, LBVIEW_WIDTH1, LBVIEW_HEIGHT1 + 10) style:UITableViewStyleGrouped];;
    
<<<<<<< HEAD
=======
    //self.rankTableV.backgroundColor = [UIColor colorWithRed:0.4 green:0.3 blue:0.5 alpha:0.4];
>>>>>>> b3b04c99b972b34aa9c958defbf325ea74670e29
    self.rankTableV.delegate = self;
    self.rankTableV.dataSource = self;
    self.rankTableV.tableHeaderView = headerView;
    [self.view addSubview:self.rankTableV];
    
    [_rankTableV release];
    [headerView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *cellId = @"reuse";
    
    RankCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        cell = [[RankCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    // JSON  1   请求图片 取到pic
    NSString *imageStr = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"pic"];
    // 2  用url接收image的地址字符串
    NSURL *imageUrl = [NSURL URLWithString:imageStr];
    // 3   打印出图片
    [cell.rankImageV setImageWithURL:imageUrl];
    
    // 取文字
    cell.rankNameLabel.text = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"rname"];
    
    cell.ranklabel.text = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"radioDesc"];
    
    cell.tagDetailLabel.text = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"tag"];
    
    cell.TimeLabel.text = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"updateStatus"];
    
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SpecialPageViewController *specialVC = [[SpecialPageViewController alloc] init];
    
    if (self.arr.count > indexPath.row) {
        
        specialVC.rid = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"rid"];
        
        specialVC.pic = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"pic"];
        
        specialVC.hostName = [[[[self.arr objectAtIndex:indexPath.row] objectForKey:@"host"] firstObject] objectForKey:@"name"];
        
        specialVC.foucsNum = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"followedNum"];
        
        specialVC.listenNum = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"listenNum"];
        
        specialVC.descRadio = [[[[[self.arr objectAtIndex:indexPath.row] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"audioDes"];
        
        specialVC.likedNum = [[[[[self.arr objectAtIndex:indexPath.row] objectForKey:@"audioList"] firstObject] objectForKey:@"audioInfo"] objectForKey:@"listenNum"];
        
        specialVC.upTime = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"updateTime"];
        
        specialVC.titleLabel = [[self.arr objectAtIndex:indexPath.row] objectForKey:@"rname"];
    }
    
    [self.navigationController pushViewController:specialVC animated:YES];
}

// TableView背景透明
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
<<<<<<< HEAD
    cell.backgroundColor = [UIColor colorWithRed:0.88 green:0.90 blue:0.738 alpha:0.4];
=======
    cell.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.2];
>>>>>>> b3b04c99b972b34aa9c958defbf325ea74670e29
}

- (void)JSONParser
{
    // 请求地址
    NSString *str = @"http://api.kaolafm.com/api/v3.5/resource/list?ids=25_&appid=0&installid=0000SZ8t&udid=17B3F9AA156016A9F3777514AC443DDF&devicetype=1&version=3.5.1&channel=appstore&timestamp=1433473528&sessionid=E90EF42780D9404E9C6CC01EA184580D&operator=1&network=1&page=200013&resolution=640*1136&sign=12B265BD3443F0F432CA252603A50BCC";
    
    str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html", nil];
    
    [manger GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        self.moguDic = [responseObject objectForKey:@"result"];
        
        self.arr = [[[self.moguDic objectForKey:@"dataList"]  firstObject] objectForKey:@"dataList"];
        
        [self.rankTableV headerEndRefreshing];
        
        //self.buttonImage1 = [self.moguDic objectForKey:@"pic"];
        
        [self.rankTableV reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}
// TableView相关设定
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
//{
//    return @"一周排行榜";
//}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
<<<<<<< HEAD
    LBView *sectionView = [[LBView alloc] init];
    sectionView.sectionLabel.textColor = [UIColor whiteColor];
    
    if (section == 0) {
        sectionView.sectionLabel.text = @"👑一周排行榜👑";
        return sectionView;
    }
    return nil;
=======
    UILabel *label = [[UILabel alloc] init];
    label.backgroundColor = [UIColor colorWithRed:0. green:0.1 blue:0.1 alpha:0.2];
    label.textAlignment = NSTextAlignmentCenter;
    label.text = @"一周排行榜";
    label.font = [UIFont boldSystemFontOfSize:19];
    label.textColor = [UIColor whiteColor];
    return [label autorelease];
>>>>>>> b3b04c99b972b34aa9c958defbf325ea74670e29
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return VIEW_HEIGHT / 6;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.f * kYHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.097 green:0.6701 blue:0.4134 alpha:1.0];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
